
package urnoted;

import java.io.File;
import java.util.*;

/**
 *
 * @author Lauren
 */
public class Song implements Comparable{
    private Note[] notes = null;
    //private final int OFFSET = 60;
    private int currIndex = 0;
    private String name = "";
    
    
    public Song(File file)
    {
        ArrayList<Note> inNotes = new ArrayList<Note>();
        Scanner scan;
        try{
        scan = new Scanner(file);
        if(scan.hasNextLine()) { name = scan.nextLine();}
        while(scan.hasNextInt())
        {
            int pitch = scan.nextInt();
            Note currNote = getNote(pitch);
            if(currNote != null){
                inNotes.add(currNote);
            }
            
        }
        notes = inNotes.toArray(new Note[0]);
        }catch(Exception e)
        {
        	System.err.println("" + e.getMessage());
        }
        
    }
    
    public Song(String inName, Note[] inNotes){
        name = inName;
        notes = inNotes;
    }
    
    public String getName(){
        return name;
    }
    
    public Note getNext()
    {
        //when the song is completed, restarts from the beginning
        if(notes == null) {return null;}
        if(currIndex+1 < notes.length) {
            currIndex++;
            return (notes[currIndex]);
        }else{
            currIndex = 0;
            return notes[0];
        }
    }
    
    public void reset(){
        currIndex = 0;
    }
    
    public Note getCurrent()
    {
        if(notes == null) {return null;}
        return notes[currIndex];
    }

    @Override
    public int compareTo(Object t) {
        if(t instanceof Song){
            Song other = (Song) t;
            return this.name.compareTo(other.getName());
        }
        return 0;
    }
    
    private Note getNote(int pitch){
        switch(pitch){
            case 60:
                return Note.C4;
            case 62:
                return Note.D4;
            case 64:
                return Note.E4;
            case 65:
                return Note.F4;
            case 67:
                return Note.G5;
            case 69:
                return Note.A5;
            case 71:
                return Note.B5;
            case 73:
                return Note.C5;
            case 75:
                return Note.D5;
            case 77:
                return Note.E5;
            case 79:
                return Note.F5;
            default:
                return null;
        }
    }
    
}
